home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / usb_diag_lib.c < prev    next >
C/C++ Source or Header  |  2001-04-11  |  9KB  |  316 lines

  1. ////////////////////////////////////////////////////////////////
  2. // File - USB_DIAG_LIB.C
  3. //
  4. // Utility functions for printing device information,
  5. // detecting USB devices
  6. // 
  7. ////////////////////////////////////////////////////////////////
  8.  
  9. #include "../../include/windrvr.h"
  10. #ifdef _USE_SPECIFIC_KERNEL_DRIVER_
  11.     #undef WD_Open
  12.     #define WD_Open WD_OpenKernelHandle
  13.     #if defined(UNIX)
  14.         #undef WD_FUNCTION
  15.         #define WD_FUNCTION(wFuncNum,h,pParam,dwSize,fWait) ((ULONG) ioctl((int)(h), wFuncNum, pParam))
  16.     #endif
  17. #endif
  18. #include "usb_diag_lib.h"
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <string.h>
  22.  
  23. char *pipeType2Str(ULONG pipeType)
  24. {
  25.     char *res = "unknown";
  26.     switch (pipeType)
  27.     {
  28.         case PIPE_TYPE_CONTROL: 
  29.             res = "Control";
  30.             break;
  31.         case PIPE_TYPE_ISOCHRONOUS:
  32.             res = "Isochronous";
  33.             break;            
  34.         case PIPE_TYPE_BULK:
  35.             res = "Bulk";
  36.             break;
  37.         case PIPE_TYPE_INTERRUPT:
  38.             res = "Interrupt";
  39.             break;
  40.     }
  41.     return res;
  42. }
  43.  
  44. // input of command from user
  45. static char line[256];
  46.  
  47. BOOL USB_Get_WD_handle(HANDLE *phWD)
  48. {
  49.     WD_VERSION ver;
  50.  
  51.     *phWD = INVALID_HANDLE_VALUE;
  52.     *phWD = WD_Open();
  53.  
  54.     // Check whether handle is valid and version OK
  55.     if (*phWD==INVALID_HANDLE_VALUE) 
  56.     {
  57.         printf("Cannot open " WD_PROD_NAME " device\n");
  58.         return FALSE;
  59.     }
  60.  
  61.     BZERO(ver);
  62.     WD_Version(*phWD, &ver);
  63.     if (ver.dwVer<WD_VER) 
  64.     {
  65.         printf("Error - incorrect " WD_PROD_NAME " version\n");
  66.         WD_Close (*phWD);
  67.         *phWD = INVALID_HANDLE_VALUE;
  68.         return FALSE;
  69.     }
  70.  
  71.     return TRUE;
  72. }
  73.  
  74. void USB_Print_device_info(DWORD dwVendorId, DWORD dwProductId)
  75. {
  76.     DWORD i;
  77.     HANDLE hWD;
  78.     WD_USB_SCAN_DEVICES usbScan;
  79.     CHAR tmp[100];
  80.  
  81.     if (!USB_Get_WD_handle (&hWD)) return;
  82.  
  83.     BZERO(usbScan);
  84.     usbScan.searchId.dwVendorId  = dwVendorId;
  85.     usbScan.searchId.dwProductId = dwProductId;
  86.     WD_UsbScanDevice(hWD,&usbScan);
  87.     for (i=0; i<usbScan.dwDevices; i++)
  88.     {
  89.         WD_USB_DEVICE_GENERAL_INFO *genInfo = &usbScan.deviceGeneralInfo[i];
  90.         printf("USB device - Vendor ID: %04x, Product ID: %04x, unique ID: %d\n", 
  91.             genInfo->deviceId.dwVendorId,
  92.             genInfo->deviceId.dwProductId,
  93.             usbScan.uniqueId[i]);
  94.         printf("      physical address: 0x%x, Hub No. %d, Port No.%d\n",
  95.             genInfo->deviceAddress, 
  96.             genInfo->dwHubNum, 
  97.             genInfo->dwPortNum);
  98.         printf("      %s speed, device has %d configuration(s)\n",
  99.             genInfo->fFullSpeed ? "Full" : "Low",
  100.             genInfo->dwConfigurationsNum);
  101.         if (genInfo->fHub)
  102.         {
  103.             printf("      Device is Hub, Hub has %d ports, %s powered, %d mA\n",
  104.                 genInfo->hubInfo.dwPorts,
  105.                 genInfo->hubInfo.fBusPowered ? "Bus": "Self",
  106.                 genInfo->hubInfo.dwHubControlCurrent);
  107.         }
  108.         printf("\n");
  109.         if (i < usbScan.dwDevices-1)
  110.         {
  111.            printf("Press Enter to continue to the next device\n");
  112.            fgets(tmp, sizeof(tmp), stdin);
  113.         }
  114.  
  115.     }
  116.     WD_Close (hWD);
  117. }
  118.  
  119. void USB_Print_all_devices_info() 
  120. {
  121.     USB_Print_device_info(0, 0);
  122. }
  123.  
  124. void USB_Print_device_Configurations()
  125. {
  126.     WD_USB_CONFIGURATION config;
  127.     DWORD i, j;
  128.     HANDLE hWD;
  129.     CHAR tmp[100];
  130.  
  131.     if (!USB_Get_WD_handle (&hWD)) return;
  132.  
  133.     BZERO (config);
  134.  
  135.     printf("Please enter the unique ID of the device:  ");
  136.     fgets(line, sizeof(line), stdin);
  137.     sscanf(line, "%d", &config.uniqueId);
  138.     printf("Please enter the configuration index to display (zero based): ");
  139.     fgets(line, sizeof(line), stdin);
  140.     sscanf(line, "%d", &config.dwConfigurationIndex);
  141.  
  142.     WD_UsbGetConfiguration(hWD, &config);
  143.  
  144.     printf("Configuration no. %d has %d interface(s)\n",
  145.         config.configuration.dwValue,
  146.         config.configuration.dwNumInterfaces);
  147.     printf("configuration attributes: 0x%x,  max power: %d mA\n",
  148.         config.configuration.dwAttributes,
  149.         config.configuration.MaxPower*2);
  150.     printf("\n");
  151.     for (i=0; i<config.dwInterfaceAlternatives; i++)
  152.     {
  153.         WD_USB_INTERFACE_DESC *pInterface = &config.Interface[i].Interface;
  154.         printf("interface no. %d,  alternate setting: %d,  index: %d\n",
  155.             pInterface->dwNumber,
  156.             pInterface->dwAlternateSetting,
  157.             pInterface->dwIndex);
  158.         printf("end-points: %d,  class: 0x%x,  sub-class: 0x%x,  protocol: 0x%x\n",
  159.             pInterface->dwNumEndpoints,
  160.             pInterface->dwClass,
  161.             pInterface->dwSubClass,
  162.             pInterface->dwProtocol);
  163.         for (j=0; j<pInterface->dwNumEndpoints; j++)
  164.         {
  165.             WD_USB_ENDPOINT_DESC *pEndPoint = &config.Interface[i].Endpoints[j];
  166.             printf("  end-point address: 0x%x, attributes: 0x%x, max packet size: %d, Interval: %d\n",
  167.                    pEndPoint->dwEndpointAddress,
  168.                    pEndPoint->dwAttributes,
  169.                    pEndPoint->dwMaxPacketSize,
  170.                    pEndPoint->dwInterval);
  171.         }
  172.         printf("\n");
  173.         if (i < config.dwInterfaceAlternatives-1)
  174.         {
  175.            printf("Press Enter to continue to the next configuration\n");
  176.            fgets(tmp, sizeof(tmp), stdin);
  177.         }
  178.  
  179.     }
  180.     WD_Close (hWD);
  181. }
  182.  
  183. #define BYTES_IN_LINE 16
  184. #define HEX_CHARS_PER_BYTE 3
  185. #define HEX_STOP_POS BYTES_IN_LINE*HEX_CHARS_PER_BYTE
  186.  
  187. void PrintHexBuffer(PVOID pBuffer, DWORD dwBytes)
  188. {
  189.     PBYTE pData = (PBYTE) pBuffer;
  190.     BYTE pHex[HEX_STOP_POS+1];
  191.     BYTE pAscii[BYTES_IN_LINE+1];
  192.     DWORD offset;
  193.     DWORD i;
  194.  
  195.     if (!dwBytes)
  196.         return;
  197.     for (offset=0; offset<dwBytes; offset++)
  198.     {
  199.         DWORD line_offset = offset%BYTES_IN_LINE;
  200.         if (offset && !line_offset)
  201.         {
  202.             pAscii[line_offset] = '\0';
  203.             printf("%s | %s\n", pHex, pAscii);
  204.         }
  205.           sprintf(pHex+line_offset*HEX_CHARS_PER_BYTE, "%02X ", pData[offset]);
  206.         pAscii[line_offset] = (pData[offset]>=0x20) ? pData[offset] : '.';
  207.     }
  208.  
  209.     // print the last line. fill with blanks if needed
  210.     if (offset%BYTES_IN_LINE)
  211.     {
  212.         for (i=(offset%BYTES_IN_LINE)*HEX_CHARS_PER_BYTE; i<BYTES_IN_LINE*HEX_CHARS_PER_BYTE; i++)
  213.             pHex[i] = ' ';
  214.         pHex[i] = '\0';
  215.     }
  216.     pAscii[offset%BYTES_IN_LINE]='\0';
  217.     printf("%s | %s\n", pHex, pAscii);
  218. }
  219.  
  220. void CloseListening(USB_LISTEN_PIPE* pListenPipe)
  221. {
  222.     WD_USB_TRANSFER transfer;
  223.     BZERO(transfer);
  224.  
  225.     if (!pListenPipe->hThread)
  226.         return;
  227.  
  228.      printf("Stop listening to pipe\n");
  229.     pListenPipe->fStopped = TRUE;
  230.  
  231.     pListenPipe->stop_pipe_func(pListenPipe->hDevice);
  232.     WaitForSingleObject(pListenPipe->hThread, INFINITE);
  233.     CloseHandle(pListenPipe->hThread);
  234.     pListenPipe->hThread = NULL;
  235. }
  236.  
  237. DWORD WINAPI PipeListenHandler(void * pParam)
  238. {
  239.     USB_LISTEN_PIPE *pListenPipe = (USB_LISTEN_PIPE*) pParam;
  240.      PVOID buf = malloc(pListenPipe->dwPacketSize);
  241.  
  242.     for (;;)
  243.     {
  244.         DWORD dwBytesTransfered = pListenPipe->read_pipe_func(pListenPipe->hDevice, buf, pListenPipe->dwPacketSize);
  245.         if(pListenPipe->fStopped)
  246.             break;
  247.         if(dwBytesTransfered==-1)
  248.         {
  249.             printf("Transfer failed\n");
  250.             break;
  251.         }
  252.         if (pListenPipe->process_data_func)
  253.             pListenPipe->process_data_func(buf, dwBytesTransfered, pListenPipe->pContext);
  254.         else
  255.             PrintHexBuffer(buf, dwBytesTransfered);
  256.     }
  257.     free(buf);
  258.     return 0;
  259. }
  260.  
  261. void ListenToPipe(USB_LISTEN_PIPE *pListenPipe)
  262. {
  263.     // start the running thread
  264.     DWORD threadId;
  265.     pListenPipe->fStopped = FALSE;
  266.     printf("Start listening to pipe\n");
  267.     pListenPipe->hThread = CreateThread (0, 0x1000, PipeListenHandler, 
  268.         (PVOID) pListenPipe, 0, &threadId);
  269. }
  270.  
  271. int GetHexChar()
  272. {
  273.     int ch;
  274.  
  275.     ch = getchar();
  276.  
  277.     if (!isxdigit(ch))
  278.         return -1;
  279.  
  280.     if (isdigit(ch))
  281.         return ch - '0';
  282.     else
  283.         return toupper(ch) - 'A' + 10;
  284. }
  285.  
  286. DWORD GetHexBuffer(PVOID pBuffer, DWORD dwBytes)
  287. {
  288.     DWORD i;
  289.     PBYTE pData = pBuffer;
  290.     BYTE res;
  291.     int ch;
  292.  
  293.     for (i=0; i<dwBytes;)
  294.     {
  295.         ch = GetHexChar();
  296.         if (ch<0)
  297.             continue;
  298.  
  299.         res = ch << 4;
  300.  
  301.         ch = GetHexChar();
  302.         if (ch<0)
  303.             continue;
  304.  
  305.         res += ch;
  306.         pData[i] = res;
  307.         i++;
  308.     }
  309.  
  310.     // advance to new line
  311.     while (ch=getchar()!=10){}
  312.  
  313.     // return the number of bytes that was read
  314.     return i;
  315. }
  316.